using System;
using System.Collections.Generic;

namespace Clandestine.Input
{
	public class KeyboardDevice : IButtonDevice
	{
		public int ButtonCount { get { return int.MaxValue; } } // actually it's uint.MaxValue
		public event internalButtonEvent ButtonPressed;
		public event internalButtonEvent ButtonReleased;
		
		protected Dictionary<int, bool> keys = new Dictionary<int, bool>(); // no need to waste loads of memory with bool[]
		
		internal KeyboardDevice() : base()
		{
		}
				
		public void OnKeyDown(int n)
		{
			if (keys.ContainsKey(n))
				keys[n] = true;
			else
				keys.Add(n, true);
		}
		
		public void OnKeyUp(int n)
		{
			if (keys.ContainsKey(n))
				keys[n] = false;
		}	
			
		public bool IsButtonPressed(int n)
		{
			lock (keys)
			{
				if ((n < 0) || (n >= this.ButtonCount))
					new ArgumentException();
					
				if (this.keys.ContainsKey(n))
					return keys[n];
				else 
					return false;
			}
		}
		
		public void ProcessEvents()
		{
			// do nothing.
		}
	}
}

